package io.dy.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.time.Duration;
import java.time.temporal.ChronoUnit;


/**
 * @author : dmj
 * @date: 2024/6/4 4:30 下午
 * @description: jedis连接池工具类
 */
public class JedisConnectionFactory {

    private static final JedisPool jedisPool;

    static {

        //1、配置连接池
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(8);
        poolConfig.setMaxIdle(8);
        poolConfig.setMinIdle(0);
        poolConfig.setMaxWait(Duration.of(1000, ChronoUnit.SECONDS));

        //2、创建连接池对象
        jedisPool = new JedisPool(poolConfig, "127.0.0.1", 6379,1000);
    }

    public static Jedis get(){
        return jedisPool.getResource();
    }
}
